package com.ysd.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.ysd.entity.Banner;
import com.ysd.entity.DesignerInfo;
import com.ysd.util.DBManager;

public class BannerDaoImpl {
	public List<Banner> getBanner() {
		List<Banner> list = new ArrayList<Banner>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs = st.executeQuery("select * from banner");
			
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String bannerimg = rs.getString("bannerimg");
				Banner gdt=new Banner(bannerId,bannerimg);
				list.add(gdt);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	/**
	 * 分页
	 * */
	public List<Banner> getBannerByPage(int page ,int size) {
		List<Banner> list = new ArrayList<Banner>();
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			String sql = "select * from banner LIMIT "+(page-1)*size+" ,"+size;
			st.executeQuery(sql);
			rs = st.executeQuery(sql);
			while(rs.next()) {
				int bannerId = rs.getInt("bannerId");
				String bannerimg = rs.getString("bannerimg");
				Banner gdt=new Banner(bannerId,bannerimg);
				list.add(gdt);
			}
			return list;
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public int delBanner(int cId) {
		return DBManager.executeUpdate("DELETE from banner where bannerId="+cId);
		
	}
	public int addBanner(Banner pc) {
		return DBManager.executeUpdate("INSERT into banner VALUES(null,'"+pc.getBannerimg()+"')");
	}
	public Banner getBannerbyid(int cateId) {
		Banner pc = null;
		Connection conn = DBManager.grtConnection();
		Statement st = null;
		ResultSet rs = null;
		try {
			st = conn.createStatement();
			rs= st.executeQuery("select * from Banner where bannerId="+cateId);
			while (rs.next()) {
				int bannerId = rs.getInt(1);
				String bannerimg = rs.getString(2);				
				pc = new Banner(bannerId,bannerimg);
			}
			return pc;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}finally {
			DBManager.closeAll(conn, st, rs);
		}
	}
	public int updateBanner(Banner pc) {
		return DBManager.executeUpdate("update Banner set bannerimg='"+pc.getBannerimg()+"' where bannerId="+pc.getBannerId());
	}

}
